//
//  ProductOrMaterialsViewCell.m
//  EastMarket
//
//  Created by 李鹏程 on 16/10/14.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "ProductOrMaterialsViewCell.h"

@implementation ProductOrMaterialsViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        CGFloat spacing = 8.0f;
        CGFloat border = 1.0f;
        CGFloat lblHeight = 24.0f;
        
        // 产品icon
        _imgProductIcon = [[UIImageView alloc] initWithFrame:CGRectMake(spacing, spacing, 110 - 2 * spacing, 110 - 2 * spacing)];
        _imgProductIcon.backgroundColor = [UIColor clearColor];
        [self addSubview:_imgProductIcon];
        
        // 产品名称
        _lblProductName = [[UILabel alloc] init];
        if (globalManager.isAllProduct) {
            
            _lblProductName.frame = CGRectMake(_imgProductIcon.frame.origin.x + spacing + _imgProductIcon.frame.size.width, spacing, SCREEN_WIDTH - _imgProductIcon.frame.origin.x - _imgProductIcon.frame.size.width - 2 * spacing, lblHeight);
        } else {
             _lblProductName.frame = CGRectMake(_imgProductIcon.frame.origin.x + spacing + _imgProductIcon.frame.size.width, spacing, SCREEN_WIDTH - _imgProductIcon.frame.origin.x - _imgProductIcon.frame.size.width - 2 * spacing - SCREEN_WIDTH / 4, lblHeight);
        }
        _lblProductName.textColor = EastMarket_darkBlackFontColor;
        _lblProductName.font = [UIFont boldSystemFontOfSize:14];
        _lblProductName.numberOfLines = 0;
        [self addSubview:_lblProductName];
        
        _lblProductPrice = [[UILabel alloc] init];
        if (globalManager.isAllProduct) {
            
            _lblProductPrice.frame = CGRectMake(_lblProductName.frame.origin.x, _lblProductName.frame.origin.y + _lblProductName.frame.size.height, (SCREEN_WIDTH - _imgProductIcon.frame.origin.x - _imgProductIcon.frame.size.width - 2 * spacing - 25 - 8) / 2, lblHeight);
        } else {
            
            _lblProductPrice.frame = CGRectMake(_lblProductName.frame.origin.x, _lblProductName.frame.origin.y + _lblProductName.frame.size.height, (SCREEN_WIDTH - _imgProductIcon.frame.origin.x - _imgProductIcon.frame.size.width - 2 * spacing - 25 - 8 - SCREEN_WIDTH / 4) / 2, lblHeight);
        }
        _lblProductPrice.textColor = EastMarket_MainToneColor;
        [_lblProductPrice setFont:[UIFont systemFontOfSize:12]];
        [self addSubview:_lblProductPrice];
        
        _lblOriginalPrice = [[UILabel alloc] initWithFrame:CGRectMake(_lblProductPrice.frame.origin.x + _lblProductPrice.frame.size.width, _lblProductPrice.frame.origin.y, _lblProductPrice.frame.size.width, _lblProductPrice.frame.size.height)];
        _lblOriginalPrice.textColor = EastMarket_darkGreyFontColor;
        [_lblOriginalPrice setFont:[UIFont systemFontOfSize:14]];
        _lblOriginalPrice.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_lblOriginalPrice];
        
        _btnFavorite = [[UIButton alloc] initWithFrame:CGRectMake(_lblOriginalPrice.frame.origin.x + _lblOriginalPrice.frame.size.width + border, _lblProductPrice.frame.origin.y, 24, 24)];
//        [_btnFavorite setBackgroundImage:[UIImage imageNamed:@"star-active"] forState:UIControlStateNormal];
        [_btnFavorite addTarget:self action:@selector(btnFavoriteOperateClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btnFavorite];
        
        _lblProductSupplier = [[UILabel alloc] initWithFrame:CGRectMake(_lblProductName.frame.origin.x, _lblProductPrice.frame.origin.y + _lblProductPrice.frame.size.height, _lblProductName.frame.size.width, _lblProductName.frame.size.height)];
        _lblProductSupplier.textColor = EastMarket_darkGreyFontColor;
        [_lblProductSupplier setFont:[UIFont systemFontOfSize:11]];
        _lblProductSupplier.numberOfLines = 0;
        [self addSubview:_lblProductSupplier];
        
        _btnNumReduce = [[UIButton alloc] initWithFrame:CGRectMake(_imgProductIcon.frame.origin.x + _imgProductIcon.frame.size.width + spacing, _lblProductSupplier.frame.origin.y + _lblProductSupplier.frame.size.height, 22.0f, 22.0f)];
        [_btnNumReduce addTarget:self action:@selector(btnNumReduceOperateClick) forControlEvents:UIControlEventTouchUpInside];
        [_btnNumReduce setTitle:@"－" forState:UIControlStateNormal];
        [_btnNumReduce setTitleColor:EastMarket_MainToneColor forState:UIControlStateNormal];
        _btnNumReduce.layer.borderWidth = border;
        _btnNumReduce.layer.borderColor = [EastMarket_hintFontColor CGColor];
        _btnNumReduce.titleLabel.font = [UIFont systemFontOfSize:14];

        [self addSubview:_btnNumReduce];
        
        _lblProductNum = [[UILabel alloc] initWithFrame:CGRectMake(_btnNumReduce.frame.origin.x + _btnNumReduce.frame.size.width, _btnNumReduce.frame.origin.y, 44, 22)];
         _lblProductNum.layer.borderColor = [EastMarket_hintFontColor CGColor];
        _lblProductNum.textColor = EastMarket_darkGreyFontColor;
        _lblProductNum.viewBorder = viewBorderTop | viewBorderBottom;
//        _lblProductNum.layer.borderWidth = border;
//        _lblProductNum.layer.borderColor = [EastMarket_hintFontColor CGColor];
        _lblProductNum.textAlignment = NSTextAlignmentCenter;
        _lblProductNum.font = [UIFont systemFontOfSize:14];
        [self addSubview:_lblProductNum];
        
        _btnNumAdd = [[UIButton alloc] initWithFrame:CGRectMake(_lblProductNum.frame.origin.x + _lblProductNum.frame.size.width, _lblProductNum.frame.origin.y, 22.0f, 22.0f)];
        [_btnNumAdd addTarget:self action:@selector(btnNumAddOperateClick) forControlEvents:UIControlEventTouchUpInside];
        _btnNumAdd.layer.borderWidth = border;
        _btnNumAdd.layer.borderColor = [EastMarket_hintFontColor CGColor];
        [_btnNumAdd setTitle:@"+" forState:UIControlStateNormal];
        [_btnNumAdd setTitleColor:EastMarket_MainToneColor forState:UIControlStateNormal];
        _btnNumAdd.titleLabel.font = [UIFont systemFontOfSize:14];

        [self addSubview:_btnNumAdd];
        
        if (!globalManager.isAllProduct) {
            _lblProductName.frame = CGRectMake(_imgProductIcon.frame.origin.x + spacing + _imgProductIcon.frame.size.width, spacing, SCREEN_WIDTH - _imgProductIcon.frame.origin.x - _imgProductIcon.frame.size.width - 2 * spacing - SCREEN_WIDTH / 4, lblHeight);
            _lblProductPrice.frame = CGRectMake(_lblProductName.frame.origin.x, _lblProductName.frame.origin.y + _lblProductName.frame.size.height, SCREEN_WIDTH - _imgProductIcon.frame.origin.x - _imgProductIcon.frame.size.width - 2 * spacing - 25 - 8 - SCREEN_WIDTH / 4, lblHeight);
            _btnFavorite.frame = CGRectMake(_lblProductPrice.frame.origin.x + _lblProductPrice.frame.size.width + border, _lblProductPrice.frame.origin.y, 24, 24);
            _lblProductSupplier.frame = CGRectMake(_lblProductName.frame.origin.x, _lblProductPrice.frame.origin.y + _lblProductPrice.frame.size.height, _lblProductName.frame.size.width, _lblProductName.frame.size.height);
        }
        
        UIView *viewSpacing = [[UIView alloc] initWithFrame:CGRectMake(_btnNumReduce.frame.origin.x,_lblProductNum.frame.origin.y + _lblProductNum.frame.size.height + spacing, SCREEN_WIDTH, 1)];
        [self addSubview:viewSpacing];
        viewSpacing.backgroundColor = EastMarket_bordColor;
    }
    
    return self;
}

- (void)btnFavoriteOperateClick {
    if (self.btnFavoriteClick) {
        self.btnFavoriteClick();
    }
}

- (void)btnNumAddOperateClick {

    CGPoint btnPoint=[self convertPoint:_btnNumAdd.center toView:self.superview];
    self.block(btnPoint);

    if (self.btnNumAddClick) {
        self.btnNumAddClick();
    }
}

- (void)btnNumReduceOperateClick {
    if (self.btnNumReduceClick) {
        self.btnNumReduceClick();
    }
}

@end
